<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br/>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容,因后台接口问题,本功能还未完成" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="orderList" border stripe style="width: 100%">
        <el-table-column type="index" label="#" min-width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" min-width="200"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)" width="160"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" min-width="180">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAddressDialog(scope.row.order_id)"></el-button> <!-- 修改按钮 -->
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressDialog(scope.row.order_id)"></el-button> <!-- 修改按钮 -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 layout指定分页器的布局结构 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10, 20]" :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>

    <!-- 修改订单地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialogVisible" width="50%" @close="editAddressDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="editAddressForm" :rules="editAddressFormRules" ref="editAddressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <!-- options用来指定数据源, props用来指定配置对象 -->
          <el-cascader v-model="editAddressForm.address1" :options="cityData" placeholder="请选择省市区/县"
                       :props="cascaderProps" @change="handleChange" clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2" placeholder="请输入详细地址"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查看物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline :reverse="true">
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
import logisticsProgress from './logisticsProgress.json'
export default {
  name: 'Order',
  data() {
    return {
      // 获取订单列表的参数对象
      queryInfo: {
        query: '', // 查询条件
        pagenum: 1, // 当前的页数
        pagesize: 10 // 每页显示多少条数据
      },
      orderList: [], // 订单列表
      total: 0, // 总数据条数

      editAddressDialogVisible: false, // 控制修改订单地址的对话框的显示与隐藏
      editAddressForm: { // 修改订单地址的表单数据
        address1: [], // 省市区/县
        address2: '' // 详细地址
      },
      editAddressFormRules: { // 添加订单地址表单的验证规则对象
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityData: cityData, // 地址数组
      cascaderProps: { // 指定级联选择器配置对象
        expandTrigger: 'hover', // 次级菜单的展开方式
        value: 'value', // 指定选项的值为选项对象的某个属性值
        label: 'label', // 指定选项标签为选项对象的某个属性值
        children: 'children' // 指定选项的子选项为选项对象的某个属性值
        // checkStrictly: true // 设置级联选择器可以选择任意一级
      },

      progressDialogVisible: false, // 控制查看物流进度的对话框的显示与隐藏
      progressInfo: [] // 查看物流进度的表单数据
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 根据分页获取对应的获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取订单列表失败')
      this.$message.success('获取订单列表表成功')
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    },

    // 监听 page-size 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },

    // 级联选择器选择项发生变化时触发这个函数
    handleChange(value) {
      // console.log(this.editAddressForm)
      console.log(value)
    },
    // 点击编辑按钮, 显示修改地址对话框
    showEditAddressDialog(id) {
      console.log(id)
      this.editAddressDialogVisible = true
    },
    // 监听修改地址对话框的关闭事件
    editAddressDialogClosed() {
      this.$refs.editAddressFormRef.resetFields()
    },
    // 点击确定按钮,提交修改地址
    editAddress() {
      // console.log(this.editAddressForm)
      this.$refs.editAddressFormRef.validate(valid => {
        if (!valid) return
        // 后台没有修改地址的API
        this.$message.success('更新地址成功')
        this.editAddressDialogVisible = false
      })
    },

    // 显示查看物流进度的对话框
    async showProgressDialog(id) {
      // 供测试的物流单号：1106975712662
      // const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      const res = logisticsProgress
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败')
      }
      this.progressInfo = res.data
      this.progressDialogVisible = true
      // console.log(this.progressInfo)
    }
  }
}
</script>

<style lang="less" scoped>
  @import "../../plugins/timeline/timeline.css"; // 导入时间线的样式
  @import "../../plugins/timeline-item/timeline-item.css";
  .el-cascader {
    width: 100%;
  }
</style>
